var goby = goby || {}

goby.PlayerState = {
    Awaking:0,
    Flying: 1,
    Ready: 2
}

goby.PlayerLayer = cc.Layer.extend({
    sprite: null,
    airFlowAngle: 0,
    airFlowCount: 0,
    awakingTime: 0,
    left: false,
    state: goby.PlayerState.Ready,
    body: null,
    space: null,
    shape: null,
    started: false,
    touched: false,
    collided: null,
    ctor: function (space, frames, startPosition) {
        this._super();
        this.space = space;
        this.sprite = new cc.PhysicsSprite(res.take_off_from_chair_1);
        var playerSize = this.sprite.getContentSize();
        // init the physic body
        this.body = new cp.Body(1, cp.momentForBox(1, playerSize.width, playerSize.height));
        // set the position
        this.body.p = startPosition;
        //this.body.sleep();//
        this.sprite.setBody(this.body);
        this.space.addBody(this.body);
        this.shape = new cp.BoxShape(this.body, playerSize.width, playerSize.height); // create the shape for the body
        this.space.addShape(this.shape);
        this.runLoops(frames, 0.2);
        this.addChild(this.sprite);
    },
    fly: function () {
        var frames = [];
        this.runLoops(frames, 0.1);
    },
    sleeping: function() {

    },
    awaking: function(){

        this.scheduleUpdate();
    },
    gameOver: function(){

    },
    runLoops: function (frames, interval) {
        var animation = this.createAnimation(frames, this.sprite.getContentSize());
        animation.setDelayPerUnit(interval);
        animation.setRestoreOriginalFrame(true);
        var action = cc.animate(animation);
        this.sprite.runAction(cc.repeatForever(action));
    },
    setCollided: function(c){
        this.collided = c;
    },
    getPlayer: function () {
        return this.sprite;
    },
    setPlayerPosition: function (position) {
        this.body.p = position;
    },
    createAnimation: function (frames, size) {
        var animation = new cc.Animation();
        if (frames instanceof Array) {
            for (var i in frames) {
                var frame = new cc.SpriteFrame(frames[i], cc.rect(0, 0, size.width, size.height));
                frame.getTexture && frame.getTexture().setAliasTexParameters();
                animation.addSpriteFrame(frame);
            }
        }
        return animation;
    },
    update: function(dt){
        if(this.touched) {
            if(this.left) this.airFlowAngle += 0.03;
            else this.airFlowAngle -= 0.03;
            this.body.setAngle(this.airFlowAngle);
            this.body.v = cp.v(50 * Math.cos(this.airFlowAngle),50 * Math.sin(this.airFlowAngle));
        }
        //if(!this.collided(this.body.p)){
        //    this.unscheduleUpdate();//Game Over, stopped!
        //}
        var timeStep = 0.05;
        this.space.step(timeStep);
    },
    onEnter: function(){
        this._super();
        cc.eventManager.addListener({
             event: cc.EventListener.TOUCH_ONE_BY_ONE,
             onTouchBegan: this.onTouchBegan,
             onTouchMove:  this.onTouchMove,
             onTouchEnded: this.onTouchEnded,
         }, this);
    },
    onTouchBegan: function(touch, event){
        var size = cc.winSize;
        var self = event.getCurrentTarget();
        if(!self.started) {
            self.awaking();
            self.started = true;
        }
        var location = touch.getLocation();
        self.left = location.x < size.width / 2;
        self.touched = true;
        //self.body.w =  self.left ? 1:-1;
        self.airFlowAngle = self.body.a;
        self.body.v = cp.v(0,0);
        return true;
    },
    onTouchMove: function(touch, event) {
    },
    onTouchEnded: function(touch, event) {
        var size = cc.winSize;
        var self = event.getCurrentTarget();
        self.touched = false;
        //self.body.w = 0;
        var angle = self.body.a;
        self.body.v = cp.v(50 * Math.cos(angle),50 * Math.sin(angle));
    },
    onExit: function(){
        this._super();
         cc.eventManager.removeListeners(cc.EventListener.TOUCH_ONE_BY_ONE);
    }
});
